<%@ page language="java" pageEncoding="utf-8"%>
<jsp:directive.page import="cn.chainco.cts.utils.TimeUtil"/>
<jsp:directive.page import="java.util.Date"/>
<%@ taglib prefix="s" uri="/struts-tags" %>
	<div class="fee_border special" style="margin-top: 5px; border: none">
		<span style="font-size:14px;">作业项目：</span>
		<table id="workItemTab1" class="tab1" cellpadding="0" cellspacing="0" style="width: 708px; margin-top: 8px;">
			<thead>
				<tr>
					<td width="80px">作业项目名称</td>
					<td width="80px">服务商</td>
					<td width="80px"><span class="stress">*</span>地点</td>
					<td width="90px"><span class="stress">*</span>开始时间</td>
					<td width="90px"><span class="stress">*</span>结束时间</td>
					<td width="100px">项目要求</td>
					<td>项目反馈</td>
					<td width="40px" align="center">故障</td>
				</tr>
			</thead>
		<s:iterator value="#request.allWorkItem" status="sta">
			<tr itemType="<s:property value="type" />" <s:if test="#sta.even">class="tr1"</s:if>>
				<td>
					<s:property value="item.name"/>
					<input type="hidden" id="workItemId<s:property value="#sta.index" />" value="<s:property value="id" />" />
					<input type="hidden" id="itemId<s:property value="#sta.index" />" value="<s:property value="item.id" />" />
				</td>
				<td>
					<s:property value="service.nickname" />
					<input type="hidden" id="serviceId<s:property value="#sta.index" />" value="<s:property value="service.id" />"  />
				</td>
				<td>
					<s:if test="work.item.type > 5">
						<span class="typePoint" title="<s:property value="work.pointORG.fullname" />"><s:property value="work.pointORG.fullname" /></span>
						<input type="hidden" id="type<s:property value="#sta.index" />" value="1" />
					</s:if>
					<s:else>
						<select id="type<s:property value="#sta.index" />" style="font-size:12px; width:98%;" class="special select_normal">
							<option value="">请选择</option>
							<option value="1" <s:if test="type == 1">selected</s:if>>出发地</option>
							<option value="2" <s:if test="type == 2">selected</s:if>>装货港</option>
							<option value="3" <s:if test="type == 3">selected</s:if>>卸货港</option>
							<option value="4" <s:if test="type == 4">selected</s:if>>目的地</option>
						</select>
					</s:else>
				</td>
				<td>
					<input type="text" id="startTime<s:property value="#sta.index" />" value="<s:date name="startTime" format="MM/dd HH:mm" />" onfocus="WdatePicker({dateFmt:'MM/dd HH:mm'})" class="input_normal special" />
				</td>
				<td>
					<input type="text" id="endTime<s:property value="#sta.index" />" value="<s:date name="endTime" format="MM/dd HH:mm" />" onfocus="WdatePicker({dateFmt:'MM/dd HH:mm'})" class="input_normal special" />
				</td>
				<td><input type="text" id="serviceRequest<s:property value="#sta.index" />" value="<s:property value="serviceRequest" />" class="input_normal special" /></td>
				<td><input type="text" id="serviceFeedback<s:property value="#sta.index" />" value="<s:property value="serviceFeedback" />" class="input_normal special" /></td>
				<td align="center"><input type="checkbox" id="status<s:property value="#sta.index" />" value="1" <s:if test="status == 1">checked="checked"</s:if> class="special"></td>
			</tr>
		</s:iterator>
		<input type="hidden" name="workFeeId" value="<s:property value="#request.workFeeId"/>"/>
		<input type="hidden" id="fees" name="fees" value="<s:property value="#request.fees"/>"/>
		<input type="hidden" name="employee1Id" value="<s:property value="#request.employee1Id"/>"/>
		<input type="hidden" id="index" name="index" value="<s:property value="#request.index"/>"/>
		<input type="hidden" id="tabindex" name="tabindex" value="<s:property value="#request.tabindex"/>"/>
		</table>
	</div>
	<s:if test="#request.editByFee == 1">
		<div style="width: 100%;text-align: right;margin-top: 10px">
	    	<input type="button" class="btn_ok" onclick="confirmItems()" value="确定" />
	    </div>
	</s:if>
	<input type="hidden" name="itemCount" value="<s:property value="#request.allWorkItem.size()" />" />
<script>
var nowDate = parseInt('<%=TimeUtil.getMM_DDHHMM(new Date()) %>'.replace(/[^\d]/g,""),10);
var firstLoad = true;
$(function(){
	/*
	for(i=1; i<=4; i++){
		$('#workItemMode tr[itemType='+i+']').appendTo($('#workItemTab'+i));
		if($('#workItemTab'+i).find('tr:gt(0)').length > 0){
			$('#workItemTab'+i).find('tr:gt(0)').each(function(i){
				if(i%2 == 0) this.className = 'tr1'
				else this.className = '';
				isCompleted($(this).find('input[name=endTime]')[0]);
				saveOrEditTr($(this).find('.fee_ok')[0]);
			})
			$('#workItemTab'+i).show();
		}else{
			$('#workItemTab'+i).hide();
		}
	}
	*/
	
	$('#workItemTab1 tr:gt(0)').find('input,select,textarea').each(function(){
		this.name = this.id;
	})
	initTypeDetail();
	firstLoad = false;
})
var itemCount = '<s:property value="#request.allWorkItem.size()" />';
function addWorkItem(type){
	var workItemTab = $('#workItemTab'+type);
	workItemTab.show();
	var itemIndex = ++itemCount;
	
	var copyWorkItemTr = $('#workItemMode tr:first').clone();
	copyWorkItemTr.attr('itemType',type);
	copyWorkItemTr.find("#service").keyup(function(){
		seachDataByKeyword('searchEmployeeBykeyword.action?type=1',this.value,'service'+itemIndex,'serviceId'+itemIndex,0,200);
	});
	copyWorkItemTr.find("#type").val(type);
	copyWorkItemTr.find('input,select').each(function(){
		this.id = this.id+itemIndex;
	})
	copyWorkItemTr.find("input[type=text]").each(function(){
		$(this).validateTips();
	})
	workItemTab.append(copyWorkItemTr);
	workItemTab.find('tr:gt(0)').each(function(i){
		if(i%2 == 0) this.className = 'tr1'
		else this.className = '';
	})
}
var preError = [];
function checkWorkItem(obj){
	var parent = $(obj);
	var itemTypeSel = parent.find('select[id*=itemType]');
	var serviceId = parent.find('input[id*=serviceId]');
	var service = parent.find('input[id*=serviceNickname]');
	var startTime = parent.find('input[id*=startTime]');
	var endTime = parent.find('input[id*=endTime]');
	var type = parent.find('select[id*=type]');
	var error = [];
	var errormsg = "";
	if(itemTypeSel.val() == 0){
		error[error.length] = itemTypeSel;
	}
	if(serviceId.val() == ''){
		error[error.length] = service;
	}
	if(startTime.val() == ''){
		error[error.length] = startTime;
		errormsg += '请选择开始日期\r\n';
	}
	if(endTime.val() == ''){
		error[error.length] = endTime;
		errormsg += '请选择结束日期\r\n';
	}
	if(startTime.val() != '' && endTime != ''){
		var st = startTime.val().replace(/[^\d]/g,"");
		var et = endTime.val().replace(/[^\d]/g,"");
		if(parseInt(st,10) > parseInt(et,10)){
			error[error.length] = startTime;
			error[error.length] = endTime;
			errormsg += '开始日期不能大于结束日期\r\n';
		}
	}
	if(type.length > 0 && type.val() == ''){
		error[error.length] = type;
		errormsg += '请选择地点\r\n';
	}
	if(error.length > 0){
		for(i=0; i< error.length; i++){
			preError[preError.length] = error[i];
			errorCss(error[i]);
		}
	}
	return errormsg;
	/*
	var resultFn = function(result){
		if(result == 0 || isNaN(result)){
			alert('保存失败')
			return false;
		}else{
			parent.find('input[id^=workItemId]').val(result);
			return true;
		}
	}
	saveOrEditTr(obj,'saveWorkItem',resultFn);
	*/
}
function delWorkItem(obj){
	var parent = $(obj).parents('tr:first');
	var workItemId = parent.find('input[id*=workItemId]').val();
	var type = parent.attr('itemType');
	var removeTr = function(){
		$(obj).parents('tr:first').find('input:text').each(function(){
			$('#tipsDiv_'+this.id).remove();
		})
		$(obj).parents('tr:first').remove();
		if($('#workItemTab'+type).find('tr:gt(0)').length > 0){
			$('#workItemTab'+type).find('tr:gt(0)').each(function(i){
				if(i%2 == 0) this.className = 'tr1'
				else this.className = '';
			})
		}else{
			$('#workItemTab'+type).hide();
		}
	}
	if(workItemId != '' || (parent.find('select[id*=itemType]').val() != 0 && parent.find('input[id*=serviceId]').val() != '')){
		if(!window.confirm('确认要删除该委托项目吗？')) return;
	}
	if(isNaN(workItemId) || workItemId == ''){
		removeTr();
		return;
	}
	$.post('cts/delWorkItem?workItemId='+workItemId,null,function(result){
		if(result == 'ok'){
			removeTr();
		}else if(result == 'error'){
			alert('该委托项目在本订单应付费用中被引用，不允许删除，请先删除相关联的应付费用！');
		}else if(result == 'error'){
			alert('删除失败，请稍候再试');
		}
	})
}

function saveAllWorkItem(){
	var errormsg = "";
	for(i=0; i<preError.length; i++){
		normalCss(preError[i]);
	}
	preError = [];
	$('#workItemTab1 tr:gt(0)').each(function(){
		var msg = checkWorkItem(this);
		if(errormsg.indexOf(msg) < 0){
			errormsg += msg;
		}
	});
	return errormsg;
}

function initTypeDetail(){
	var point1 = '<s:property value="work.pointORG.fullname" />';
	var point2 = '<s:property value="work.pointPOL.fullname" />';
	var point3 = '<s:property value="work.pointPOD.fullname" />';
	var point4 = '<s:property value="work.pointDES.fullname" />';
	var point5 = '<s:property value="work.expressExportCountry.fullname" />';
	var point6 = '<s:property value="work.expressImportCountry.fullname" />';
	if($('#type').val() == 4){
		point1 = point5;
		point4 = point6;
	}
	setTypeDetail(point1,point2,point3,point4);
}

function changeTypeDetail(){
	if($('#type').val() > 5){
		$('.typePoint').html($('#pointORG').val());
		$('.typePoint').attr('title',$('#pointORG').val());
		return;
	}
	var point1 = $('#pointORG'), point2 = $('#pointPOL'), point3 = $('#pointPOD'), point4 = $('#pointDES');
	if($('#type').val() == 4){
		point1 = $('#expressExportCountry');
		point4 = $('#expressImportCountry');
	}
	point1 = point1.val() || '';
	point2 = point2.val() || '';
	point3 = point3.val() || '';
	point4 = point4.val() || '';
	setTypeDetail(point1,point2,point3,point4);
}

function setTypeDetail(point1,point2,point3,point4){
	var options = [];
	options[options.length] = new Option('请选择','');
	if(point1 != '' && point1 != point2){
		options[options.length] = new Option(point1,1);
	}
	if(point2 != ''){
		options[options.length] = new Option(point2,2);
	}
	if(point3 != ''){
		options[options.length] = new Option(point3,3);
	}
	if(point4 != '' && point3 != point4){
		options[options.length] = new Option(point4,4);
	}
	$('select[id*=type]').each(function(){
		var value = this.value;
		if(value == 1 && point1 == point2){
			value = 2;
		}else if(value == 4 && point3 == point4){
			value = 3;
		}
		this.options.length = 0;
		for(i=0; i<options.length; i++){
			this.options.add(new Option(options[i].text,options[i].value));
		}
		if(value != ''){
			this.value = value;
		}
	})
}

function isCompleted(obj){
	var parent = $(obj).parents('tr:first');
	var select = parent.find('select[id*=status]')[0];
	var date = parseInt(obj.value.replace(/[^\d]/g,""),10);
	if(date < nowDate){
		select.options[0].text = '完成';
	}else{
		select.options[0].text = '进行';
	}
}
</script>